package com.auto.learning.view;

import java.util.Map;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.auto.learning.R;

public class MyPopWindowDialog {
	private Context context;
	public TextView  tv_rep,tv_share,tv_delete;
	private LinearLayout layout;
	private Map<String, Object> map;
	private IPopDialogOnclickInterface dialogOnclickInterface;
	private PopupWindow popupWindow;
	private int[] location;
	private View parentView,view_right,view_left;
	
	public MyPopWindowDialog(Context context,Map<String, Object> map,int[] location,View parentView) {
		this.context = context;
		this.map = map;
		this.location = location;
		this.parentView = parentView;
	}
	
	public void setOnClickDelete(IPopDialogOnclickInterface dialogOnclickInterface){
		this.dialogOnclickInterface = dialogOnclickInterface;
	}

	public void showDialog(){
		View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
		layout = (LinearLayout) view.findViewById(R.id.rel_main);
		view_right = view.findViewById(R.id.view_right);
		view_left = view.findViewById(R.id.view_left);
		tv_share = (TextView) view.findViewById(R.id.tv_share);
		tv_share.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				dialogOnclickInterface.OnShareClick(map);
			}
		});
		tv_rep = (TextView) view.findViewById(R.id.tv_rep);
		tv_rep.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				dialogOnclickInterface.OnRepClick(map);
			}
		});
		tv_delete = (TextView) view.findViewById(R.id.tv_delete);
		tv_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				dialogOnclickInterface.onDeleteClick(map);
			}
		});
		if (map.get("isSelf").toString().equals("1")) {
			tv_delete.setVisibility(View.GONE);
			view_right.setVisibility(View.GONE);
			tv_rep.setVisibility(View.VISIBLE);
			view_left.setVisibility(View.VISIBLE);
		} else {
			tv_delete.setVisibility(View.VISIBLE);
			view_right.setVisibility(View.VISIBLE);
			tv_rep.setVisibility(View.GONE);
			view_left.setVisibility(View.GONE);
		}
		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		popupWindow.showAtLocation(parentView, Gravity.TOP, location[0], location[1]);
	}
	
	public interface IPopDialogOnclickInterface {
		void OnShareClick(Map<String, Object> map);
		void OnRepClick(Map<String, Object> map);
		void onDeleteClick(Map<String, Object> map);
	}
}
